home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / EXCEPT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.9 KB  |  265 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.9  $
  6. //
  7. // Implementation of class TXOwl, the base exception class for OWL exceptions
  8. // that can forward handling to the app module by default.
  9. //----------------------------------------------------------------------------
  10. #include <owl/pch.h>
  11. #if !defined(OWL_WINDOW_H)
  12. # include <owl/window.h>
  13. #endif
  14. #if !defined(OWL_EXCEPT_H)
  15. # include <owl/except.h>
  16. #endif
  17. #if !defined(OWL_MODULE_H)
  18. # include <owl/module.h>
  19. #endif
  20. #include <stdio.h>
  21.  
  22. OWL_DIAGINFO;
  23.  
  24. //
  25. // mbModalFlag() determines the best MB modal flag to use in the current
  26. // situation. Uses MB_TASKMODAL if under NT, or the task/thread has at least
  27. // one toplevel window. Uses MB_SYSTEMMODAL for Win32S/Win16 when there are
  28. // no toplevel windows.
  29. //
  30. bool CALLBACK hasWndProc(TWindow::THandle, bool far* hasWnd)
  31. {
  32.   *hasWnd = true;
  33.   return false;
  34. }
  35.  
  36. static unsigned mbModalFlag()
  37. {
  38. #if defined(BI_PLAT_WIN32)
  39.   // NT can always open task modal, even before windows are up
  40.   //
  41.   if (TSystem::IsNT())
  42.     return MB_TASKMODAL;
  43. #endif
  44.  
  45.   // Windows95, Win32s & Win16 need first window created before task modal msg
  46.   // box can be created
  47.   //
  48.   bool hasWnds = false;
  49. #if defined(BI_PLAT_WIN32)
  50.   ::EnumThreadWindows(GetCurrentThreadId(), (WNDENUMPROC)hasWndProc,
  51.                       TParam2(&hasWnds));
  52. #else
  53.   ::EnumTaskWindows(GetCurrentTask(), (WNDENUMPROC)hasWndProc,
  54.                     TParam2((bool far*)&hasWnds));
  55. #endif
  56.  
  57.   return hasWnds ? MB_TASKMODAL : MB_SYSTEMMODAL;
  58. }
  59.  
  60. #if defined(BI_NAMESPACE)
  61. namespace OWL {
  62. #endif 
  63. //
  64. // Global exception handler used when an application object is not available.
  65. // May be overriden by user code by redefining this function. Note that the
  66. // program must be linked statically to OWL in order for an overridden version
  67. // of this function to be called by the framework. If a valid
  68. // application object is found by GetApplicationObject, then the virtual
  69. // TModule::Error(TXOwl& x, char* caption, bool canResume) is usually used
  70. // instead.
  71. //
  72. int _OWLFUNC
  73. HandleGlobalException(xmsg& x, char* caption, char* canResume)
  74. {
  75.   char errorStr[255];
  76.   int  buttons = MB_OK;
  77.   int  len = x.why().length();
  78.  
  79.   if (!caption)
  80.     caption = "Unhandled Exception";
  81.   if (len)
  82.     strcpy(errorStr, x.why().c_str());
  83.   else {
  84.     strcpy(errorStr, "Unknown Exception");
  85.     len = strlen(errorStr);
  86.   }
  87.   if (canResume) {
  88.     buttons = MB_YESNO;
  89.     errorStr[len] = '\n';
  90.     strcpy(errorStr+len+1, canResume);
  91.   }
  92.   return ::MessageBox(0, errorStr, caption,
  93.                       mbModalFlag() | MB_ICONSTOP | buttons) == IDYES ? 0 : -1;
  94. }
  95.  
  96. #if defined(BI_NAMESPACE)
  97. } // namespace OWL
  98. #endif 
  99.  
  100. //----------------------------------------------------------------------------
  101.  
  102. //
  103. // An OWL exception with a given message for displaying and an unsigned Id
  104. // that can be used for identification or loading a string
  105. //
  106. TXOwl::TXOwl(const string& msg, uint resId)
  107. :
  108.   TXBase(msg),
  109.   ResId(resId)
  110. {
  111. }
  112.  
  113. //
  114. // An OWL exception with a given unsigned Id that can is used for loading a
  115. // message string & identification
  116. //
  117. TXOwl::TXOwl(unsigned resId, TModule* module)
  118. :
  119.   TXBase(ResourceIdToString(0, resId, module)),
  120.   ResId(resId)
  121. {
  122. }
  123.  
  124. //
  125. //
  126. //
  127. TXOwl::~TXOwl()
  128. {
  129. }
  130.  
  131. //
  132. //
  133. //
  134. int
  135. TXOwl::Unhandled(TModule* app, uint promptResId)
  136. {
  137.   return app->Error(*this, IDS_OWLEXCEPTION, promptResId);
  138. }
  139.  
  140. #if defined(BI_NO_COVAR_RET)
  141. TXBase*
  142. #else
  143. TXOwl*
  144. #endif
  145. TXOwl::Clone()
  146. {
  147.   return new TXOwl(*this);
  148. }
  149.  
  150. //
  151. //
  152. //
  153. void
  154. TXOwl::Throw()
  155. {
  156.   THROW( *this );
  157. }
  158.  
  159. //
  160. // Construct a TXOwl exception from scratch, and throw it. Two versions
  161. // corresponding to the two constructor signatures
  162. //
  163. void
  164. TXOwl::Raise(const string& msg, uint resId)
  165. {
  166.   TXOwl(msg, resId).Throw();
  167. }
  168.  
  169. //
  170. //
  171. //
  172. void
  173. TXOwl::Raise(uint resId, TModule* module)
  174. {
  175.   TXOwl(resId, module).Throw();
  176. }
  177.  
  178. //
  179. // Static member function used to convert a resource id to a 'string'. This
  180. // is necessary since we must pass a string to the xmsg base class
  181. // constructor.  Sets found to true if the resource was located, otherwise
  182. // false.  In either case, the string is initialized to something
  183. // printable.
  184. //
  185. string
  186. TXOwl::ResourceIdToString(bool* found, uint resId, TModule* module)
  187. {
  188.   char buf[128];
  189.  
  190.   bool status = module && module->LoadString(resId, buf, sizeof buf);
  191.   if (found)
  192.     *found = status;
  193.  
  194.   if (!status)
  195.     sprintf(buf, "Exception #%u (Could not load description string; <owl/except.rc> not bound?).", resId);
  196.  
  197.   string rscStr(buf);
  198.   return rscStr;
  199. }
  200.  
  201. //
  202. // Extension to string loader adds the feature of sprintf'ing an
  203. // additional information string into the resource message string.
  204. //
  205. string
  206. TXOwl::MakeMessage(uint resId, const char far* infoStr, TModule* module)
  207. {
  208.   string rscMsg = ResourceIdToString(0, resId, module);
  209.   char buf[255];
  210.   sprintf(buf, rscMsg.c_str(), infoStr);
  211.   return string(buf);
  212. }
  213.  
  214. //
  215. // Extension to string loader adds the feature of sprintf'ing an additional
  216. // information number into the resource message string.
  217. //
  218. string
  219. TXOwl::MakeMessage(uint resId, uint infoNum, TModule* module)
  220. {
  221.   string rscMsg = ResourceIdToString(0, resId, module);
  222.   char buf[255];
  223.   sprintf(buf, rscMsg.c_str(), infoNum);
  224.   return string(buf);
  225. }
  226.  
  227. //----------------------------------------------------------------------------
  228.  
  229. //
  230. //
  231. //
  232. TXOutOfMemory::TXOutOfMemory()
  233. :
  234.   TXOwl(IDS_OUTOFMEMORY)
  235. {
  236. }
  237.  
  238. #if defined(BI_NO_COVAR_RET)
  239. TXBase*
  240. #else
  241. TXOutOfMemory*
  242. #endif
  243. TXOutOfMemory::Clone()
  244. {
  245.   return new TXOutOfMemory(*this);
  246. }
  247.  
  248. //
  249. //
  250. //
  251. void
  252. TXOutOfMemory::Throw()
  253. {
  254.   THROW( *this );
  255. }
  256.  
  257. //
  258. // Construct a TXOutOfMemory exception from scratch, and throw it
  259. //
  260. void
  261. TXOutOfMemory::Raise()
  262. {
  263.   TXOutOfMemory().Throw();
  264. }
  265.